!function () {
    function request(url,method,data,success){
        $$.ajax({
            method:method,
            url:url,
            data:data,
            dataType:'json',
            success:success
        });
    }
    function getWenjuanQuestion()
    {
        request(Get_wenjuan_question_url,'GET',{wenjuanid:Globalwjid},function (res) {
            //console.log(res);
            Wjapp.hidePreloader();
            if (res.code==1)
            {
                var html=Wjapp.QitemTemplate(res.data);
                $$('#Wenjuan ul.wenjuan-content-list').html(html);

                Globalqnum=res.data.qdata.length;
                //设定背景
                $$('#Wenjuan .page-content').css("background-image","url('"+Globalbg+"')");
                //设定前言
                $$('.wenjuan-intro').html(Globalintro);
                //显示提交按钮
                $$('.submit-btn').removeClass('hidden');
            }
        });
    }
    function getUserRealName() {
        Wjapp.prompt('请输入您的姓名','',function (name) {
            name = name.replace(/\s*/g, "");
            if (!name) {
                Wjapp.alert('输入的姓名不能为空','',getUserRealName);
                return;
            }
            Wjapp.showPreloader('提交中');
            request(Submit_name_url,'POST',{name:name,uid:Globaluid},function (res) {
                // console.log(res);
                if (res.code == 1) {
                    getWenjuanQuestion();
                }
            });
        },function () {
            getUserRealName();
        });
    }
    function checkUserComplished(url,wenjuanid)
    {
        $$.ajax({
            method:'GET',
            url:url,
            data:{wenjuanid:wenjuanid},
            dataType:'json',
            xhrFields:{"withCredentials":true},
            success:function (res) {
                //console.log(res);
                if (res.code==1){
                    Globaluid=parseInt(res.data.uid);
                    Globalbg=res.data.bgurl;
                    Globalintro=res.data.intro;
                    wx.onMenuShareAppMessage({
                        title: res.data.sharetitle,
                        desc: res.data.sharedesc,
                        link: "http://api.nldou.com/ucenter/weixinlogin?pid=25381&page=&param=wenjuanid."+wenjuanid,
                        imgUrl: res.data.shareimg,
                        type: 'link'
                    });
                    Wjapp.showPreloader('加载中');
                    //用户已经填写
                    if (res.data.status==2) {
                        Wjapp.hidePreloader();
                        mainView.router.load({"url":"resultpage.html","reload":true,"context":{"ansdata":res.data.ansdata,"restype":res.data.restype,"bgurl":Globalbg}});
                        // mainView.router.load({"url":"resultpage.html","context":{"ansok":res.data.ansok,"ansoknum":res.data.ansoknum
                        //     ,"ansno":res.data.ansno,"ansnonum":res.data.ansnonum}});
                    }
                    //问卷已经截止且用户没有填写
                    else if (res.data.status==3){
                        window.location.href="http://api.nldou.com/wenjuan/invalid";
                    }else{
                        Wjapp.hidePreloader();
                        getUserRealName();
                    }
                }else{window.location.href="http://api.nldou.com/ucenter/weixinlogin?pid=25381&page=&param=wenjuanid."+wenjuanid;}
            }
        });
    }

    var $$=Dom7;
    var Globalbg,Globalintro;
    var Globalans=[];
    var Globalanscount=0;
    var Globalwjid,Globaluid,Globalqnum;
    var query = window.location.search;
    if (query.indexOf('wenjuanid=')===-1) window.location.href="http://api.nldou.com/wenjuan/invalid";
    else Globalwjid=query.substr((query.indexOf('wenjuanid=')+10));

    var Initjssdk_url = "http://api.nldou.com/weixinapi/init_wx_jsapi";
    var Get_wenjuan_question_url = "http://api.nldou.com/wenjuan/getWenjuanQuestion";
    var Check_wenjuan_complished_url = "http://api.nldou.com/wenjuan/checkUserComplish";
    var Submit_ans_url = "http://api.nldou.com/wenjuan/submitAns";
    var Submit_name_url = "http://api.nldou.com/wenjuan/submitName";

    checkUserComplished(Check_wenjuan_complished_url,Globalwjid);

    $$.ajax({
        url:Initjssdk_url,
        method:"GET",
        data: {fromUrl:window.location.href},
        dataType:'json',
        success: function (res) {
            if(res.code==1){
                wx.config({
                    debug: false,
                    appId: res.data.appid,
                    timestamp: res.data.timestamp,
                    nonceStr: res.data.nonceStr,
                    signature: res.data.signature,
                    jsApiList: ['hideMenuItems','onMenuShareAppMessage']
                });
            }
        }
    });

    wx.ready(function () {
        wx.hideMenuItems({
            menuList: ['menuItem:share:timeline','menuItem:share:qq'
                ,'menuItem:share:weiboApp', 'menuItem:share:facebook', 'menuItem:share:QZone'
                ,'menuItem:copyUrl', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari']
        });

    });

    /****Initialize your app****/
    var Wjapp = new Framework7({
        modalTitle:'',
        modalButtonOk:'确认',
        modalButtonCancel:'取消',
        template7Pages:true,
        swipeBackPage:false
    });

    /****Add view****/
    var mainView = Wjapp.addView('.view-main', {});

    Wjapp.QitemTemplate=Template7.compile($$('script#Qitem').html());

    $$(document).on('click','.btn',function () {
        var box=$$(this).parent();
        //选中选项
        box.find('.btn').removeClass('active');
        $$(this).addClass('active');
        //存储答案
        var ans = {};
        ans.key=$$(this).data('key');
        ans.qid=parseInt($$(this).data('qid'));
        ans.sort=parseInt($$(this).data('sort'))-1;
        // var ansort=parseInt($$(this).data('sort'))-1;

        //之前没有回答过该题
        if (!Globalans[ans.sort]) Globalanscount++;
        Globalans[ans.sort]=ans;
        //console.log(Globalans,Globalanscount);
    });
    $$(document).on('click','.submit-btn',function () {
        //console.log(Globalanscount);
        Wjapp.showPreloader('提交中');
        if (Globalanscount==Globalqnum) {
            request(Submit_ans_url,'POST',{uid:Globaluid,wenjuanid:Globalwjid,ans:Globalans},function (res) {
                //console.log(res.data);
                if (res.code==1){
                    Wjapp.hidePreloader();
                    mainView.router.load({"url":"resultpage.html","reload":true,"context":{"restype":res.data.restype,"ansdata":res.data.ansdata,"bgurl":Globalbg}});
                }else{
                    Wjapp.hidePreloader();
                    Wjapp.alert('系统繁忙请重试','');
                }
            });
        }else{
            Wjapp.hidePreloader();
            Wjapp.alert('请完成所有题目','');
        }
    });
}();
